<?php
namespace app\manage\controller;
use think\Db;

class ChannelBag extends Base
{
    //列表
    public function index(){
        if( request()->isAjax() ){
            $page = input('page');
            $limit = input('limit');
            $gameId = input('game_id');
            $id = input('id');

            $where['status'] = ['<>', -1];
            !empty($gameId) && $where['game_id'] = $gameId;
            !empty($id) && $where['id'] = $id;

            if( ! $count=model('ChannelBag')->where($where)->count() ){
                $this->layTable();
            }

            $select = model('ChannelBag')
                ->where($where)
                ->order('add_time desc')
                ->limit(($page-1)*$limit, $limit)
                ->select();

            $data = [];
            $gameIdName = model('Game')->gameIdName();
            $channelBagType = changeValKey('ChannelBagType', 'id', 'type');
            foreach ($select as $k=>$v){
                $data[] = [
                    'id' => $v['id'],
                    'add_type' => $v['add_type'],
                    'game_id' => $v['game_id'],
                    'game_name' => $gameIdName[ $v['game_id'] ],
                    'name' => $v['name'],
                    'type_id' => $channelBagType[ $v['type_id'] ],
                    'total_num' => $v['total_num'],
                    'give_num' => $v['give_num'],
                    'keep_num' => $v['keep_num'],
                    'explain' => $v['explain'],
                    'add_time' => $v['add_time'],
                    'status' => $v['status'],
                ];
            }
            $this->layTable(0, '渠道礼包列表', $data, $count);
        }else{
            return view();
        }
    }

    //添加
    public function add(){
        if( request()->isAjax() ){
            $data = input();

            $where['add_type'] = $data['add_type'];
            $where['type_id'] = $data['type_id'];
            $where['game_id'] = $data['game_id'];
            $where['status'] = 1;
            if( model('ChannelBag')->where($where)->count() ){
                $this->layJson(1, '已存在该游戏该类型礼包');
            }

            if($data['add_type']==1 && $data['file']){ //文件上传
                $data['file_name'] = $data['file'];
                unset($data['one_file']);
                unset($data['file']);

                $filePath = ROOT_PATH.'uploads/bag/channel/';
                import('cut.Io');
                $Io = new \Io( $filePath );
                $data['total_num'] = $data['keep_num'] = $Io->fileCountLines( $filePath . $data['file_name'] );
            }else if($data['add_type']==2){ //单个礼包码
                unset($data['file']);

                $data['total_num'] = $data['keep_num'] = $data['give_num'] = 1;
            }else{
                $this->layJson(1, '请上传礼包码');
            }

            $data['status'] = 1;
            $addRs = model('ChannelBag')->save( $data );
            $addRs ? $this->layJson(0, '添加成功') : $this->layJson(1, '添加失败，请重试');
        }else{
            $typeId = model('ChannelBagType')->select();
            return view('', ['type_id'=>$typeId]);
        }
    }

    //编辑
    public function edit(){
        if( request()->isAjax() ){
            $editRs = model('ChannelBag')->allowField(true)->isUpdate(true)->save( input() );
            $editRs!==false ? $this->layJson(0, '编辑成功') : $this->layJson(1, model('Channel')->getError());
        }else{
            $find = model('ChannelBag')->where('id', input('id'))->find();
            return view('', ['find'=>$find]);
        }
    }

    //删除
    public function del(){
        $delRs = model('ChannelBag')->allowField(true)->isUpdate(true)->save(['status'=>-1], ['id'=>input('id')]);
        $delRs!==false ? $this->layJson(0, '删除成功') : $this->layJson(1, '删除失败，请重试');
    }

    //追加
    public function append(){
        if( request()->isAjax() ){
            if($_FILES['file']['error'] !== 0){
                $this->layJson(1, "文件上传失败！");
            }
            $fileName = input('file_name');
            $tmpName = $_FILES['file']['tmp_name'];
            $filePath = ROOT_PATH.'uploads/bag/channel/';
            import('cut.Io');
            $Io = new \Io($filePath);
            $rs = $Io->addToFile($fileName, $tmpName);
            if(!$rs['status']) {
                $this->layJson(1, $rs['message']);
            }

            //追加完成修改总数量，可领取数量
            $fileNum = $Io->fileCountLines($tmpName);
            $rs1 = model('ChannelBag')->where('id', input('id'))->setInc('total_num', $fileNum);
            $rs2 = model('ChannelBag')->where('id', input('id'))->setInc('keep_num', $fileNum);

            $rs1&&$rs2 ? $this->layJson(0, '追加成功') : $this->layJson(1, model('Channel')->getError());
        }else{
            $find = model('ChannelBag')->where('id', input('id'))->find();
            $gameIdName = model('Game')->gameIdName();
            $find['game_name'] = $gameIdName[ $find['game_id'] ];
            return view('', ['find'=>$find]);
        }
    }

    //裁剪
    public function cut(){
        if( request()->isPost() ){
            $cutNum = input('cut_num');
            if(!is_numeric($cutNum) || !is_int($cutNum+0)){
                $this->error('裁剪数量必须是整数');
            }
            $find = model('ChannelBag')->field('file_name,keep_num')->where('id', input('id'))->find();

            if($find['keep_num'] < $cutNum){
                $this->error('礼包数量不足');
            }

            $filePath = ROOT_PATH.'uploads/bag/channel/';
            import('cut.Io');
            $Io = new \Io($filePath);
            $cutRs = $Io->cutFileMulti($find['file_name'], $find['file_name'], $cutNum);
            if($cutRs['status']){
               model('ChannelBag')->where('id', input('id'))->setDec('keep_num', $cutNum);
            }else{
                $this->error($cutRs['message']);
            }
        }else{
            $find = model('ChannelBag')->where('id', input('id'))->find();
            $gameIdName = model('Game')->gameIdName();
            $find['game_name'] = $gameIdName[ $find['game_id'] ];
            return view('', ['find'=>$find]);
        }
    }
}